package Server.DataBase;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 *class handle data save in table
 */
public class Info extends Query{

	public Info() {
		super();
	}
	/**
	 * set a new user id
	 * @return new user id num
	 *         else 0 if error 
	 */	
	public int getNewUserId() throws SQLException{
		setQuery("SELECT * FROM info " + "WHERE name = ?");
		PreparedStatement query = getPS();
		query.setString(1, "lastUser");
		ResultSet rs = query.executeQuery();
		if (rs.next()) return rs.getInt(3) + 1;
		else return 0;
	}
	/**
	 *
	 * inc user id
	 */
	public void incUserId() throws SQLException{
		setQuery("UPDATE info "+"SET data = ? WHERE name = ? ");
		PreparedStatement update = getPS();
		update.setInt(1, getNewUserId());
		update.setString(2, "lastUser");
		update.executeUpdate();
		update.close();
	}
	/**
	 * set a new class id
	 * @return new class id num
	 *         else 0 if error 
	 */		
	public int getNewClassId() throws SQLException{
		setQuery("SELECT * FROM info " + "WHERE name = ?");
		PreparedStatement query = getPS();
		query.setString(1, "lastClass");
		ResultSet rs = query.executeQuery();
		if (rs.next()) return rs.getInt(3) + 1;
		else return 0;
	}
	/**
	 *
	 * inc class id
	 */
	public void incClassId() throws SQLException{
		setQuery("UPDATE info "+"SET data = ? WHERE name = ? ");
		PreparedStatement update = getPS();
		update.setInt(1, getNewClassId());
		update.setString(2, "lastClass");
		update.executeUpdate();
		update.close();
	}
	/**
	 * set a new semester id
	 * @return new semester id num
	 *         else 0 if error 
	 */		
	public int getNewSemesterId() throws SQLException{
		setQuery("SELECT * FROM info " + "WHERE name = ?");
		PreparedStatement query = getPS();
		query.setString(1, "currentSemester");
		ResultSet rs = query.executeQuery();
		if (rs.next()) return rs.getInt(3) + 1;
		else return 0;
	}
	/**
	 * 
	 * @return current semster id
	 */		
	public int getCurrentSemesterId() throws SQLException{
		setQuery("SELECT * FROM info " + "WHERE name = ?");
		PreparedStatement query = getPS();
		query.setString(1, "currentSemester");
		ResultSet rs = query.executeQuery();
		if (rs.next()) return rs.getInt(3);
		else return 0;
	}
	/**
	 *
	 * inc semester id
	 */
	public void incSemesterId() throws SQLException{
		setQuery("UPDATE info "+"SET data = ? WHERE name = ? ");
		PreparedStatement update = getPS();
		update.setInt(1, getNewSemesterId());
		update.setString(2, "currentSemester");
		update.executeUpdate();
		update.close();
	}
	/**
	 * set a new lesson id
	 * @return new lesson id num
	 *         else 0 if error 
	 */		
	public int getNewLessonId() throws SQLException{
		setQuery("SELECT * FROM info " + "WHERE name = ?");
		PreparedStatement query = getPS();
		query.setString(1, "lastLesson");
		ResultSet rs = query.executeQuery();
		if (rs.next()) return rs.getInt(3)+1;
		else return 0;
	}
	/**
	 *
	 * inc lesson id
	 */
	public void incLessonId() throws SQLException{
		setQuery("UPDATE info "+"SET data = ? WHERE name = ? ");
		PreparedStatement update = getPS();
		update.setInt(1, getNewLessonId());
		update.setString(2, "lastLesson");
		update.executeUpdate();
		update.close();
	}
	/**
	 * set a new reg id
	 * @return new reg id num
	 *         else 0 if error 
	 */		
	public int getNewRegId() throws SQLException{
		setQuery("SELECT * FROM info WHERE name = 'lastReg'");
		ResultSet rs = execQuery();
		if (rs.next()){
			setQuery("UPDATE info SET data = ? WHERE name = 'lastReg'");
			PreparedStatement update = getPS();
			update.setInt(1, rs.getInt(3)+1);
			update.executeUpdate();
			update.close();
			return rs.getInt(3)+1;
		}
		else return 0;
	}	
	/**
	 * set a new request id
	 * @return new request id num
	 *         else 0 if error 
	 */
	public int getNewRequestId() throws SQLException{
		setQuery("SELECT * FROM info WHERE name = 'lastRequest'");
		ResultSet rs = execQuery();
		if (rs.next()){
			setQuery("UPDATE info SET data = ? WHERE name = 'lastRequest'");
			PreparedStatement update = getPS();
			update.setInt(1, rs.getInt(3)+1);
			update.executeUpdate();
			update.close();
			return rs.getInt(3)+1;
		}
		else return 0;
	}	
}
